const fs = require('fs')
const path = require('path')

// 写所有的日志
function writeLog(writeSteam, log) {
    writeSteam.write(log + '\n') // 关键代码
}

// 生成 write stream(第二个水桶)
function createWriteStream(fileName, flags) {
    // 找到真正的日志文件的地址
    const fullFileName = path.join(__dirname, '../', '../', 'logs', fileName)
    const writeSteam = fs.createWriteStream(fullFileName, {
        // 给它添加一个标识符
        flags
    })
    return writeSteam
}

// 写访问成功的日志
const accessWriteStream = createWriteStream('access.log', 'a')
function access(log) {
    writeLog(accessWriteStream,log)
}

// // 写访问失败的日志
// const errorWriteStream = createWriteStream('error.log', 'e')
// function error(log) {
//     writeLog(errorWriteStream, log)
// }

module.exports = {
    access,
    // error
}